<template>
  <div class="user-avatar-dropdown">
    <Dropdown @on-click="handleClick">
      <Badge :dot="!!messageUnreadCount">
        <Avatar :src="userAvatar" />
      </Badge>
      <!-- <Icon :size="18" type="md-arrow-dropdown"></Icon> -->
      <Button size="small" type="text">
        {{ $store.state.user.userName }}
        <Icon type="md-arrow-dropdown"></Icon>
      </Button>
      <DropdownMenu slot="list">
        <DropdownItem v-if="isAdmin" name="menuManage">{{$t('v7xKzJGr2izTFUrwho0jj')}}</DropdownItem>
        <DropdownItem v-if="isAdmin" name="roleManage">{{$t('EXLDIwEt8jA5HgdjovPwx')}}</DropdownItem>
        <DropdownItem name="modifyPassword">{{$t('modify_password')}}</DropdownItem>
        <!-- <DropdownItem name="message">
          {{$t('ZkJOHDGViY5LaMLOz11Sd')}}<Badge
            style="margin-left: 10px"
            :count="messageUnreadCount"
          ></Badge>
        </DropdownItem> -->
        <DropdownItem name="logout">{{$t('osPaW1xFg2DtSzu4ZwC6I')}}</DropdownItem>
      </DropdownMenu>
    </Dropdown>
  </div>
</template>

<script>
import './user.less'
import { mapActions } from 'vuex'
import iView from 'iview'
export default {
  name: 'User',
  props: {
    userAvatar: {
      type: String,
      default: ''
    },
    messageUnreadCount: {
      type: Number,
      default: 0
    }
  },
  computed: {
    isAdmin () {
      return this.$store.getters.isAdmin
    }
  },
  methods: {
    ...mapActions(['handleLogOut']),
    logout () {
      iView.LoadingBar.start()
      this.handleLogOut().then(
        res => {
          if (res.code != 200) {
            this.$Message.error(res.desc)
            return
          }
          this.$router.push({
            name: 'login'
          })
        },
        err => {
          this.$Message.error('退出账号失败，' + err)
        }
      ).finally(() => {
        iView.LoadingBar.finish()
      })
    },
    message () {
      this.$router.push({
        name: 'message_page'
      })
    },
    handleClick (name) {
      switch (name) {
        case 'menuManage':
          this.$router.push({ name: 'menu_manage' })
          break
        case 'roleManage':
          this.$router.push({ name: 'default_role_manage' })
          break
        case 'modifyPassword':
          this.$router.push({ name: 'modify_password' })
          break
        case 'logout':
          this.logout()
          break
        case 'message':
          this.message()
          break
      }
    }
  }
}
</script>
